#include "hudcamera.h"

#include <osgText/Text>
#include <osg/NodeCallback>
#include <osg/Depth>
#include <osgDB/ReadFile>
class HUDCallback : public osg::NodeCallback
{
public:
	HUDCallback(){}

	HUDCallback(const HUDCallback& nc,const osg::CopyOp& copyOp)
		:NodeCallback(nc,copyOp) {}

	virtual void operator()(osg::Node* node, osg::NodeVisitor* nv)
	{ 

		if (mMainCamera.valid() && mMT.valid())
		{
			osg::Matrix mat = mMainCamera->getViewMatrix();
			mat.setTrans(0,0,-6);
			mMT->setMatrix(mat);
			
			//osg::notify(osg::FATAL)<<"OK"<<std::endl;
		}
		traverse(node,nv);
	}
	osg::observer_ptr< osg::Camera > mMainCamera;
	osg::observer_ptr< osg::MatrixTransform > mMT;

};

osg::Geode* HudCamera::createAxis( float size )
{
	osg::ref_ptr<osg::Geode> root = new osg::Geode;

	osg::Geometry* geometry = new osg::Geometry();

	osg::Vec3Array* vertices (new osg::Vec3Array());
	vertices->push_back (osg::Vec3 ( 0.0, 0.0, 0.0));
	vertices->push_back (osg::Vec3 ( size, 0.0, 0.0));
	vertices->push_back (osg::Vec3 ( 0.0, 0.0, 0.0));
	vertices->push_back (osg::Vec3 ( 0.0, size, 0.0));
	vertices->push_back (osg::Vec3 ( 0.0, 0.0, 0.0));
	vertices->push_back (osg::Vec3 ( 0.0, 0.0, size));
	geometry->setVertexArray (vertices);

	osg::Vec4Array* colors (new osg::Vec4Array());
	colors->push_back (osg::Vec4 (1.0f, 0.0f, 0.0f, 1.0f));
	colors->push_back (osg::Vec4 (1.0f, 0.0f, 0.0f, 1.0f));
	colors->push_back (osg::Vec4 (0.0f, 1.0f, 0.0f, 1.0f));
	colors->push_back (osg::Vec4 (0.0f, 1.0f, 0.0f, 1.0f));
	colors->push_back (osg::Vec4 (0.0f, 0.0f, 1.0f, 1.0f));
	colors->push_back (osg::Vec4 (0.0f, 0.0f, 1.0f, 1.0f));
	geometry->setColorArray (colors);

	geometry->setColorBinding (osg::Geometry::BIND_PER_VERTEX);    
	geometry->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::LINES,0,6));

	root->addDrawable( geometry );
	root->getOrCreateStateSet()->setMode(GL_LIGHTING,false);
	root->getOrCreateStateSet()->setAttributeAndModes(new osg::Depth(osg::Depth::LESS,0.0,0.0001));


	if (true)
	{
		osgText::Font *font = osgText::readFontFile("fonts/arial.ttf");

		osgText::Text* tx = new osgText::Text();
		tx->setText("x");
		tx->setFont(font);
		tx->setColor(osg::Vec4(1,0,0,1));
		tx->setCharacterSize(24.0);
		tx->setFontResolution(32,32);
		tx->setPosition(osg::Vec3(1,0,0));
		tx->setAutoRotateToScreen(true);
		tx->setCharacterSizeMode(osgText::TextBase::SCREEN_COORDS);
		tx->setAlignment(osgText::TextBase::CENTER_BOTTOM);
		root->addDrawable( tx );

		osgText::Text* ty = new osgText::Text();
		ty->setText("y");
		ty->setFont(font);
		ty->setColor(osg::Vec4(0,1,0,1));
		ty->setCharacterSize(24.0);
		ty->setFontResolution(32,32);
		ty->setPosition(osg::Vec3(0,1,0));
		ty->setAutoRotateToScreen(true);
		ty->setCharacterSizeMode(osgText::TextBase::SCREEN_COORDS);
		ty->setAlignment(osgText::TextBase::CENTER_BOTTOM);
		root->addDrawable( ty );

		osgText::Text* tz = new osgText::Text();
		tz->setText("z");
		tz->setFont(font);
		tz->setColor(osg::Vec4(0,0,1,1));
		tz->setCharacterSize(24.0);
		tz->setFontResolution(32,32);
		tz->setPosition(osg::Vec3(0,0,1));
		tz->setAutoRotateToScreen(true);
		tz->setCharacterSizeMode(osgText::TextBase::SCREEN_COORDS);
		tz->setAlignment(osgText::TextBase::CENTER_BOTTOM);
		root->addDrawable( tz );
	}

	return root.release();
}

HudCamera::HudCamera( osg::Camera* main_camera, int width, int height )
{
	_mainCamera = main_camera;

	_hudCamera = new osg::Camera;
	_hudCamera->setRenderOrder(osg::Camera::POST_RENDER,10);
	_hudCamera->setClearMask(0);
	_hudCamera->setReferenceFrame(osg::Transform::ABSOLUTE_RF);

	_hudCamera->setProjectionMatrixAsPerspective(30,1,0.1,10.0);

	_mt = new osg::MatrixTransform;
	_mt->addChild(createAxis());
	_hudCamera->addChild(_mt.get());
	_mainCamera->addChild(_hudCamera.get());

	HUDCallback* callback = new HUDCallback;
	callback->mMainCamera = _mainCamera;
	callback->mMT = _mt;

	_hudCamera->setCullCallback(callback);

	_hudCamera->setViewport(0,0,width,height);
}

HudCamera::~HudCamera()
{

}
